<template>
  <div class="Centerbar">
    <dv-loading style="color: aqua" v-if="loading">加载中</dv-loading>
    <div ref="app" class="echarts" v-else></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
// import { getWorkshopDayList } from "@/api/index";
export default {
  name: "bottomBar",
  props: {
    workOrderList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      xdata: [],
      ydata1: [],
      loading: false,
      screenWidth: document.body.clientWidth,
      screenHeight: document.body.clientHeight,
      myChart: "",
      timer: null, // 定时器
    };
  },

  methods: {
    getrequests() {
      this.xdata = [];
      this.ydata1 = [];
      let data = this.workOrderList;
      this.ydata1 = data.map((item) => {
        return Math.round((item.number / item.workOrderNum) * 100);
      });
      this.xdata = data.map((item) => {
        return item.workOrderCode;
      });
      this.$nextTick(() => {
        this.getdata();
      });
    },
    getdata() {
      let that = this;
      var myChart = echarts.init(this.$refs.app);
      myChart.clear();
      clearInterval(this.timer);
      this.myChart = myChart;
      var option;
      let dataX = this.xdata;
      let datY = this.ydata1;
      option = {
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: "1%",
          right: "4%",
          bottom: "3%",
          //   top: "7%",
          containLabel: true,
        },
        xAxis: {
          name: "工单号",
          type: "category",
          data: dataX,
          nameTextStyle: {
            color: "#a6cac9",
            fontSize: 15,
            padding: [0, 0, 0, -10],
          },
          axisLabel: {
            rotate: 30,
            textStyle: {
              color: "#a6cac9",
              fontSize: 15,
            },
          },
        },
        yAxis: {
          name: "百分比(%)",
          type: "value",
          max: 100, //最大值
          min: 0, //最小值
          interval: 20, //间隔
          axisTick: {
            show: false,
          },
          nameTextStyle: {
            color: "#a6cac9",
            fontSize: 14,
          },
          axisLabel: {
            textStyle: {
              color: "#a6cac9",
              fontSize: 15,
            },
          },
        },
        series: [
          {
            name: "百分比(%)",
            type: "bar",
            color: {
              type: "linear",
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: "#10FA9A" }, // 0% 处的颜色
                { offset: 1, color: "#0A9C60" }, // 100% 处的颜色
              ],
              global: false, // 缺省为 false
            },
            data: datY,
            label: {
              show: true,
              position: "top",
              color: "#93A3F5",
              fontSize: 16,
              formatter: "{c}" + "%",
            },
            barWidth: "50%",
          },
        ],
        //   //自动滚动
        dataZoom: [
          //滑动条
          {
            show: false, //是否显示滑动条
            type: "slider", // 这个 dataZoom 组件是 slider 型 dataZoom 组件
            startValue: 0, // 从头开始。
            endValue: 18, // 一次性展示5个。
          },
          {
            type: "slider", // 这里可以选择你需要的dataZoom类型
            start: 0, // 滑动条开始位置（百分比）
            end: 100, // 滑动条结束位置（百分比）
            height: 9,
            bottom: 3,
          },
        ],
      };
      this.timer = setInterval(function () {
        // 每次向后滚动一个，最后一个从头开始。
        if (option.dataZoom[0].endValue == dataX.length - 1) {
          option.dataZoom[0].endValue = 18;
          option.dataZoom[0].startValue = 0;
        } else {
          option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1;
          option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1;
        }
        myChart.setOption(option, true);
      }, 5000);

      window.addEventListener("resize", () => {
        //使用resize监听方法
        that.screenWidth = document.body.clientWidth;
        window.screenWidth = that.screenWidth;
        that.screenHeight = document.body.clientHeight;
        window.screenHeight = that.screenHeight;
      });
      option && myChart.setOption(option, true);
    },
  },
  mounted() {
    this.getrequests();
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  watch: {
    screenWidth() {
      this.myChart.resize();
    },
    screenHeight() {
      this.myChart.resize();
    },
  },
};
</script>
<style scoped>
.echarts {
  width: 100%;
  height: 40vh;
}
</style>
